﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.SECURITY.MODEL;

namespace KPIS.GERP.SECURITY.DAL
{
    public class RoleDAL
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public RoleDAL()
        {

        }
        public DataTable LoadRoles(Nullable<int> Sequence, string System)
        {
            string strSQL;
            
            DataTable dtb = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@ActiveRecord", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(1, "@Sequence", Sequence);
                dbManager.AddParameters(2, "@System", System);

                strSQL = "SELECT ROLE_SEQ, ROLE_NAME, SYSTEM AS SYSTEM_ID, REMARKS"
                    + " FROM SYS_SC_ROLES"
                    + " WHERE RECORD_STATUS = @ActiveRecord"
                    + (System.Length > 0 ? " AND SYSTEM = @System" : "")
                    + (Sequence.HasValue ? " AND ROLE_SEQ = @Sequence" : "");

                dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }

            return dtb;
        }

        public string SelectRecentInsertedRoleSeq()
        {
            string strSQL;
            string strResult;

            try
            {
                dbManager.Open();

                strSQL = "select last_insert_id() as role_seq from roles";
                //strSQL = "select role_seq, role_name, system, remarks"
                //    + " from roles"
                //    + " where created_when = " + Misc.ValueForSQL(created_when);

                strResult = "";// Misc.StringValue(dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows[0]["role_seq"]);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }

            return strResult;
        }

        public void Insert(string RoleName, string System, string Remarks, int CreatedBy)
        {
            try
            {
                string strSQL;
                dbManager.Open();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@RoleName", RoleName);
                dbManager.AddParameters(1, "@System", System);
                dbManager.AddParameters(2, "@Remarks", Remarks);
                dbManager.AddParameters(3, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(4, "@CreatedBy", CreatedBy);
                dbManager.AddParameters(5, "@CreatedWhen", DateTime.Now);

                strSQL = "INSERT INTO SYS_SC_ROLES"
                    + "(ROLE_NAME, SYSTEM, REMARKS, RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES (@RoleName, @System, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";
         
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Update(int Sequence, string RoleName, string System, string Remarks, int UpdatedBy)
        {            
            try
            {
                string strSQL;

                dbManager.Open();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@RoleName", RoleName);
                dbManager.AddParameters(2, "@System", System);
                dbManager.AddParameters(3, "@Remarks", Remarks);
                dbManager.AddParameters(4, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(5, "@UpdatedWhen", DateTime.Now);

                strSQL = "UPDATE SYS_SC_ROLES"
                    + " SET ROLE_NAME = @RoleName"
                        + ", SYSTEM = @System"
                        + ", REMARKS = @Remarks"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE ROLE_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        public void Delete(int Sequence, int UpdatedBy)
        {
            try
            {
                string strSQL;

                dbManager.Open();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Now);

                strSQL = "UPDATE SYS_SC_ROLES"
                    + " SET RECORD_STATUS = @DeleteRecord"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE ROLE_SEQ = @Sequence";
                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
            }
            catch
            {
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
